I tried using multiple Intrinsic function together in AWS CloudFormation

I tried using multiple Intrinsic function together in AWS CloudFormation

Clock Icon2023.03.24

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

I was working with a CloudFormation i want to use multiple intrinsic function together so i tried and shared bellow:

Introduction:

AWS CloudFormation is a powerful infrastructure-as-code service that allows you to define and deploy cloud resources in a repeatable and automated manner One unique feature that CloudFormation offers is Fn::Sub, It allows you to replace variables in a string with their corresponding values. This is very useful for dynamically generating resource names, URLs, and other values based on the current context of the CloudFormation stack.

Fn::Sub Function

The Fn::Sub function replaces variables in the input string with specified values. You can use the Fn::Sub function to replace supported functions such as Fn::FindInMap, Fn::ImportValue, or to replace variables in the input string. The syntax for the Fn::Sub function is:


{ "Fn::Sub" : [ String, { Var1Name: Var1Value, Var2Name: Var2Value } ] }


The first parameter is a string containing the variables to be assigned. The second parameter is an object that specifies the name and value of each variable to be assigned. Let's take a look at some examples of using the Fn::Sub function in functions that support it.

Usage of Fn::Sub with Fn::FindInMap function

The Fn::FindInMap function returns the value corresponding to the specified key in the specified map. The Fn::FindInMap function has the following syntax:


{ "Fn::FindInMap" : [ MapName, TopLevelKey, SecondLevelKey ] }


In the following example, the Fn::Sub and Fn::FindInMap functions are used to substitute the variable log_group_name with the value of the mapping result:


{
  "Mappings": {
    "LogGroupMapping": {
      "Test": {
        "Name": "test_log_group"
      },
      "Prod": {
        "Name": "prod_log_group"
      }
    }
  },
  "Resources": {
    "myLogGroup": {
      "Type": "AWS::Logs::LogGroup",
      "Properties": {
        "LogGroupName": {
          "Fn::Sub": [
            "cloud_watch_${log_group_name}",
            {
              "log_group_name": {
                "Fn::FindInMap": [
                  "LogGroupMapping",
                  "Test",
                  "Name"
                ]
              }
            }
          ]
        }
      }
    }
  }
}


This example uses the Fn::FindInMap function to find the "Name" value in the "LogGroupMapping" map. Then use the Fn::Sub function to replace the log_group_name variable with the resulting value of the mapping.

Usage of Fn::Sub with Fn::ImportValue function

The Fn::ImportValue function returns the value of an exported output from another CloudFormation stack. The syntax for the Fn::ImportValue function is:


{ "Fn::ImportValue" : "exported-output-name" }

 

The following example uses the Fn::Sub function with the Fn::ImportValue function to replace the Domain variable with a value derived from the imported value.

{
  "Resources": {
    "DNS": {
      "Type": "AWS::Route53::HostedZone",
      "Properties": {
        "Name": {
          "Fn::Sub": [
            "www.${Domain}",
            {
              "Domain": {
                "Fn::ImportValue": "DomainName"
              }
            }
          ]
        }
      }
    }
  }
}
``

Reference:

https://aws.amazon.com/premiumsupport/knowledge-center/cloudformation-fn-sub-function/

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.